﻿using NPOI.HSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace NB_Controller_Test
{
    /// <summary>
    /// CreateGPRSInfo.xaml 的交互逻辑
    /// </summary>
    public partial class CreateGPRSInfo : Window
    {
        private List<string> strIndexList = new List<string>();
        private List<Point> ptGPRSList = new List<Point>();

        public class Alias2Gprs
        {
            public string strAliasName;
            public Point ptGprs;
        }

        public List<Alias2Gprs> alias2GprsList = new List<Alias2Gprs>();

        public CreateGPRSInfo()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            bool bSaveToExcel = cbSaveToExcel.IsChecked == true;

            //根据界面输入值，计算总共有多少个，每一个得名称是什么，各自得GPRS参数是多少
            string strFirstIndex = tbFirst_Index.Text;
            string strFirstJingDu = tbFirst_JingDu.Text;
            string strFirstWeiDu = tbFirst_WeiDu.Text;

            string strEndIndex = tbEnd_Index.Text;
            string strEndJingDu = tbEnd_JingDu.Text;
            string strEndWeiDu = tbEnd_WeiDu.Text;

            string strIndexStep = tbIndex_Step.Text;

            UInt16 uiIndexStep = GlobalFun.StringToUInt16(strIndexStep);
            if(uiIndexStep == UInt16.MaxValue /*|| uiIndexStep == UInt16.MinValue*/)
            {
                uiIndexStep = 1;
            }

            if(strFirstIndex.Length <= 0 || strFirstJingDu.Length <= 0 || strFirstWeiDu.Length <= 0 || 
                strEndIndex.Length <= 0 || strEndJingDu.Length <= 0 || strEndWeiDu.Length <= 0 )
            {
                MessageBox.Show("有必填数据为空，请确认！");
            }
            else
            {
                strIndexList.Clear();
                ptGPRSList.Clear();
                alias2GprsList.Clear();

                //根据输入，计算命名和GPRS信息
                int nCount = CalcAliasName(strFirstIndex, strEndIndex, uiIndexStep);

                CalcGPRSInfo(strFirstJingDu, strFirstWeiDu, strEndJingDu, strEndWeiDu, nCount);

                for (int i = 0; i < nCount; i++)
                {
                    Alias2Gprs alias2Gprs = new Alias2Gprs();
                    alias2Gprs.strAliasName = strIndexList[i];
                    alias2Gprs.ptGprs = ptGPRSList[i];

                    alias2GprsList.Add(alias2Gprs);
                }

                if (bSaveToExcel == true && nCount > 0)
                {
                    SaveToExcel();                    

                    MessageBox.Show("Excel保存成功！");
                }
            }
            
        }

        private int CalcAliasName(string strFirstIndex, string strEndIndex, UInt16 uiIndexStep, string strSplitTag = "_")
        {
            int nFirstIndex = strFirstIndex.LastIndexOf(strSplitTag);
            string strFirstPerInfo = strFirstIndex.Substring(0, nFirstIndex);
            string strFirstIndexValue = strFirstIndex.Substring(nFirstIndex + 1);

            int nEndIndex = strEndIndex.LastIndexOf(strSplitTag);
            string strEndPerInfo = strEndIndex.Substring(0, nEndIndex);
            string strEndIndexValue = strEndIndex.Substring(nEndIndex + 1);

            if(strFirstPerInfo != strEndPerInfo)
            {
                //名称不统一
                MessageBox.Show("名称格式不统一，请确认！");
            }
            else
            {
                UInt16 uiFirstValue = GlobalFun.StringToUInt16(strFirstIndexValue);
                UInt16 uiEndValue = GlobalFun.StringToUInt16(strEndIndexValue);

                int nCount = (uiEndValue - uiFirstValue) / uiIndexStep;

                strIndexList.Add(tbTotalName.Text + strFirstIndex);
                for (int i = 1; i < nCount; i++)
                {
                    strIndexList.Add(tbTotalName.Text + strFirstPerInfo + strSplitTag + (i * uiIndexStep + 1).ToString());
                }
                if (nCount > 1)
                    strIndexList.Add(tbTotalName.Text + strEndIndex);
                //最后一个主动替换成界面设置得
                //strIndexList[strIndexList.Count - 1] = strEndIndex;
            }

            return strIndexList.Count;
        }

        private void CalcGPRSInfo(string strFirstJingDu, string strFirstWeiDu, string strEndJingDu, string strEndWeiDu, int nCount)
        { 
            Double dFirstJingDu = GlobalFun.StringToDouble(strFirstJingDu);
            Double dFirstWeiDu = GlobalFun.StringToDouble(strFirstWeiDu);

            Double dEndJingDu = GlobalFun.StringToDouble(strEndJingDu);
            Double dEndWeiDu = GlobalFun.StringToDouble(strEndWeiDu);

            Double dJingDuStep = (dEndJingDu - dFirstJingDu) / (nCount - 1);
            Double dWeiDuStep = (dEndWeiDu - dFirstWeiDu) / (nCount - 1);

            for (int i = 0; i < nCount; i++ )
            {
                Point pt = new Point();
                pt.X = dFirstJingDu + i * dJingDuStep;
                pt.Y = dFirstWeiDu + i * dWeiDuStep;

                ptGPRSList.Add(pt);
            }
        }

        private void SaveToExcel()
        {
            System.Windows.Forms.SaveFileDialog saveDlg = new System.Windows.Forms.SaveFileDialog();
            saveDlg.InitialDirectory = @"C:\";
            //设置保存的文件的类型，注意过滤器的语法
            saveDlg.Filter = "Excel文档|*.xls|所有文件|*.*";
            saveDlg.FileName = string.Format("{0:yyyy-MM-dd_HH-mm-ss}", System.DateTime.Now) + "_AlisaGPRSInfo.xls";// 结果为：2010-6-19 20:30

            //调用ShowDialog()方法显示该对话框，该方法的返回值代表用户是否点击了确定按钮
            if (saveDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                //MessageBox.Show("保存成功");
                string strFilePath = saveDlg.FileName;
                File.Delete(strFilePath);
                //GlobalFun.WriteExcel(dataTable_rsrp, strFilePath);

                DataTable dataTable_rsrp = new DataTable("别名和经纬度信息表");
                dataTable_rsrp.Columns.Add(new DataColumn("序号", typeof(string)));
                dataTable_rsrp.Columns.Add(new DataColumn("别名", typeof(string)));
                dataTable_rsrp.Columns.Add(new DataColumn("经度", typeof(string)));
                dataTable_rsrp.Columns.Add(new DataColumn("纬度", typeof(string)));

                int nIndex = 0;
                foreach(string strIndex in strIndexList)
                {
                    string strName = tbTotalName.Text + strIndex;
                    dataTable_rsrp.Rows.Add((nIndex + 1).ToString(), strName, ptGPRSList[nIndex].X.ToString(), ptGPRSList[nIndex].Y.ToString());

                    nIndex++;
                }

                GlobalFun.WriteExcel(dataTable_rsrp, strFilePath);
            }
            else
            {
                //MessageBox.Show("取消保存");
            }
        }
    }
}
